<template>
    <div class="app-container">
        <el-card shadow="always" v-show="showSearch">
            <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" @submit.native.prevent>
                <el-form-item label="分类名称" prop="classifyName">
                    <el-input v-model="queryParams.classifyName" placeholder="请输入分类名称" clearable
                        @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleQuery">搜索</el-button>
                    <el-button @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card class="box-card" shadow="always" :body-style="{ padding: '20px' }">
            <!-- <div slot="header">
            <span></span>
        </div> -->
            <!-- card body -->
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" v-hasPermi="['order:goodsCategory:add']" @click="handleAdd">添加分类
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" v-hasPermi="['order:goodsCategory:add']" :disabled="!selectedRows.length"
                        @click="handleBatchDelete">删除
                    </el-button>
                </el-col>
                <right-toolbar :v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-if="refreshTable" row-key="id" :default-expand-all="isExpandAll"
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" v-loading="loading"
                :data="dataSource" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" :selectable="selectHandle" />
                <el-table-column label="分类名称" align="left" prop="classifyName" />
                <el-table-column label="排序" align="left" prop="sort" width="150" />
                <el-table-column label="创建时间" align="left" prop="createTime" />
                <el-table-column label="状态" align="left" prop="classStatus">
                    <template #default="scope">
                        <el-tag :type="scope.row.classStatus == 1 ? 'success' : 'danger'">
                            {{ scope.row.classStatus == 1 ? '正常' : '隐藏' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="left">
                    <template #default="scope">
                        <el-button v-hasPermi="['order:goodsCategory:add']" type="text"
                            @click="handleAdd(scope.row)">新增</el-button>
                        <el-divider v-hasPermi="['order:goodsCategory:add']" direction="vertical"></el-divider>
                        <el-button v-hasPermi="['order:goodsCategory:edit']" type="text"
                            @click="handleEdit(scope.row)">编辑</el-button>
                        <el-divider v-hasPermi="['order:goodsCategory:edit']" direction="vertical"></el-divider>
                        <el-button v-hasPermi="['order:goodsCategory:remove']" type="text"
                            @click="handleRowDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- <pagination v-show="total > 0" :total="total" v-model:page.sync="queryParams.pageNum"
                v-model:limit.sync="queryParams.pageSize" @pagination="getList" /> -->
        </el-card>
        <addForm ref="modelForm" :categoryList="categoryList" @ok="getList"></addForm>
    </div>
</template>

<script>
import addForm from './modules/addForm.vue';
import listMixin from '@/mixins/listMixin';
import request from '@/utils/request'
export default {
    name: "category",
    components: {
        addForm
    },
    mixins: [listMixin],
    data() {
        return {
            queryParams: {
                pageNum: 1,
                pageSize: 10
            },
            urls: {
                list: '/order/goodsCategory/allList',
                delete: '/order/goodsCategory',
            },
            isExpandAll: true,
            refreshTable: true,
        }
    },
    methods: {
        /**
         * @获取列表
         */
        getList() {
            this.loading = true;
            var params = {
                url: this.urls.list,
                method: 'get',
                params: this.getQueryParams() //查询条件
            }

            request(params).then(res => {
                this.loading = false;
                this.dataSource = this.handleTree(res, 'id');
            });
        },

        /** 展开/折叠操作 */
        toggleExpandAll() {
            this.refreshTable = false;
            this.isExpandAll = !this.isExpandAll;
            this.$nextTick(() => {
                this.refreshTable = true;
            });
        },

        /**
         * @批量删除
         */
        handleBatchDelete() {
            if (this.selectedRows.length == 0) {
                this.$modal.msgError('请选择要删除的数据');
                return;
            }
            if (this.selectedRows.some(item => item.children && item.children.length > 0)) {
                this.$modal.msgError('请先删除子分类');
                return;
            }
            var ids = this.selectedRows.map(item => item.id).join(',');
            this.handleDelete(ids);
        },

        handleRowDelete(row) {
            if (row.children && row.children.length > 0) {
                this.$modal.msgError('请先删除子分类');
                return;
            }
            this.$modal.confirm('确定删除？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                request({
                    url: this.urls.delete + '/' + row.id,
                    method: 'delete'
                }).then(res => {
                    if (res.code == 200) {
                        this.$modal.msgSuccess('删除成功')
                        this.getList()
                    } else {
                        this.$modal.msgError(res.msg)
                    }
                });
            }).catch(() => { });
        },
    },
};
</script>
<style type="text/css" scoped>
.box-card {
    margin-top: 10px;
}
</style>